﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Fuse8.ContentManagementFramework.Domain;

namespace Fuse8.ContentManagementFramework.BizFacade.Services.Contents.Queryable
{
    internal class CmsContentQueryable : IOrderedQueryable<GenericCmsEntity>
    {
        public int ContentTypeId { get; protected set; }
        public bool IgnoreRevisionsSlicer { get; set; }

        public Expression Expression { get; private set; }

        public CmsContentQueryable(int contentTypeId, bool ignoreRevisionsSlicer = false)
        {
            ContentTypeId = contentTypeId;
            Expression = Expression.Constant(this);
            IgnoreRevisionsSlicer = ignoreRevisionsSlicer;
        }

        public CmsContentQueryable(int contentTypeId, Expression expression, bool ignoreRevisionsSlicer = false)
        {
            Expression = expression;
            ContentTypeId = contentTypeId;
            IgnoreRevisionsSlicer = ignoreRevisionsSlicer;
        }

        public IEnumerator<GenericCmsEntity> GetEnumerator()
        {
            return Provider.Execute<IEnumerable<GenericCmsEntity>>(Expression).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return Provider.Execute<IEnumerable<GenericCmsEntity>>(Expression).GetEnumerator();
        }
        
        public Type ElementType
        {
            get { return typeof (GenericCmsEntity); }
        }

        public IQueryProvider Provider
        {
            get { return new CmsContentQueryProvider(ContentTypeId, IgnoreRevisionsSlicer); }
        }        
    }
}